package spms;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan(basePackages = "spms.front")
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        List<RequestParameter> parameters = new ArrayList<>();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("spms.front.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(generateGlobalRequestParameters());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("spms-front").description("API接口示例").version("1.0").build();
    }

    private List<RequestParameter> generateGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();

        //the token param.
        RequestParameterBuilder tokenParam = new RequestParameterBuilder();
        tokenParam.name("x-token").description("Access Token").required(false).in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)));

        parameters.add(tokenParam.build());

        //
        return parameters;
    }
}
